﻿namespace ManagerClinic.CanLamSang
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Linq;
    using System.Windows.Forms;
    using DevExpress.XtraEditors;
    using System.Configuration;
    using ManagerClinic.Ultilites.HtmlEditor.Buttons;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Modules;
    using ManagerClinic.Ultilities.Ultility;

    public partial class frmPhieuThamDoChucNang : DevExpress.XtraEditors.XtraForm
    {
        string _MaThamDoChucNang = string.Empty, _MaHoSoKhamBenh = string.Empty, _MaDichVu = string.Empty;
        Int32 _status;
        bool resultInsert = false;
        public event EventHandler _SavePhieuCanLamSang = null;

        HoSoKhamBenhNgoaiTruEntity hoSoKhamBenhNgoaiTruEntity = null;
        ThuPhiDichVuBusiness thuPhiDichVuBusiness = new ThuPhiDichVuBusiness();
        PhieuThamDoChucNangBusiness phieuThamDoChucNangBusiness = new PhieuThamDoChucNangBusiness();
        PhieuThamDoChucNangEntity phieuThamDoChucNangEntity = null;
        PhieuThamDoChucNangChiTietBusiness phieuThamDoChucNangChiTietBusiness = new PhieuThamDoChucNangChiTietBusiness();
        DmBacSyBusiness dmBacSyBusiness = new DmBacSyBusiness();
        HoSoKhamBenhNgoaiTruBusiness hoSoKhamBenhNgoaiTruBusiness = new HoSoKhamBenhNgoaiTruBusiness();

        public frmPhieuThamDoChucNang(Int32 status, string maDichVu, HoSoKhamBenhNgoaiTruEntity obj)
        {
            InitializeComponent();

            PredefinedButtonSets.SetupDefaultButtons(this.txtKetQua);
            PredefinedButtonSets.SetupDefaultButtons(this.txtKetLuan);

            _status = status;
            resultInsert = false;
            hoSoKhamBenhNgoaiTruEntity = obj;
            _MaDichVu = maDichVu;
            _MaThamDoChucNang = ConstantManagerClinic.setCodeManagerClinic(ConstantManagerClinic.maPhieuThamDoCN);

            ucThongTinHanhChinh.LoadThongTinHanhChinh(hoSoKhamBenhNgoaiTruEntity.MaBenhNhan);
            ucChiDinhKhoaPhong.LoadDmKhoaKham();
            ucChiDinhKhoaPhong.LoadDmKhoaCanLamSang();
            ucChiDinhKhoaPhong.LoadDmPhongKham();
            ucChiDinhKhoaPhong.LoadDmPhongCanLamSang();
            ucChiDinhKhoaPhong.LoadDmDichVuChiTiet(_MaDichVu);

        }

        public frmPhieuThamDoChucNang(Int32 status, string maThamDoChucNang)
        {
            InitializeComponent();

            PredefinedButtonSets.SetupDefaultButtons(this.txtKetQua);
            PredefinedButtonSets.SetupDefaultButtons(this.txtKetLuan);

            _status = status;
            resultInsert = true;
            _MaThamDoChucNang = maThamDoChucNang;
            phieuThamDoChucNangEntity = phieuThamDoChucNangBusiness.GetPhieuThamDoChucNang(_MaThamDoChucNang);
            hoSoKhamBenhNgoaiTruEntity = hoSoKhamBenhNgoaiTruBusiness.GetHoSoKhamBenhNgoaiTru(phieuThamDoChucNangEntity.MaHoSo);

            _MaDichVu = phieuThamDoChucNangEntity.MaDichVu;
            ucThongTinHanhChinh.LoadThongTinHanhChinh(phieuThamDoChucNangEntity.MaBenhNhan);

            LoadInfoPhieu(phieuThamDoChucNangEntity);

            ucChiDinhKhoaPhong.LoadDmKhoaKham();
            ucChiDinhKhoaPhong.LoadDmKhoaCanLamSang();
            ucChiDinhKhoaPhong.LoadDmPhongKham();
            ucChiDinhKhoaPhong.LoadDmPhongCanLamSang();
            ucChiDinhKhoaPhong.setMaKhoaKham(phieuThamDoChucNangEntity.MaKhoaKham);
            ucChiDinhKhoaPhong.setMaPhongKhamBenh(phieuThamDoChucNangEntity.MaPhongKham);
            ucChiDinhKhoaPhong.setMaKhoaCanLamSang(phieuThamDoChucNangEntity.MaKhoaCanLamSang);
            ucChiDinhKhoaPhong.setMaPhongCanLamSang(phieuThamDoChucNangEntity.MaPhongCanLamSang);
            ucChiDinhKhoaPhong.LoadDmDichVuChiTiet(_MaDichVu);
        }

        void LoadInfoPhieu(PhieuThamDoChucNangEntity obj)
        {
            ucChiDinhKhoaPhong.setMaKhoaKham(obj.MaKhoaKham);
            ucChiDinhKhoaPhong.setMaPhongKhamBenh(obj.MaPhongKham);
            ucChiDinhKhoaPhong.setMaKhoaCanLamSang(obj.MaKhoaCanLamSang);
            ucChiDinhKhoaPhong.setMaPhongCanLamSang(obj.MaPhongCanLamSang);
            ucChiDinhKhoaPhong.setBacSy(obj.MaBacSyKham);

            txtKetQua.Html = obj.KetQua;
            txtKetLuan.Html = obj.KetLuan;
            txtAnh1.Text = obj.Anh1;
            txtAnh2.Text = obj.Anh2;
            txtAnh3.Text = obj.Anh3;
            txtAnh4.Text = obj.Anh4;

            var dmBacSyClsEntity = dmBacSyBusiness.GetDmBacSy(obj.MaBacSyCls);
            if (dmBacSyClsEntity != null)
                txtBacSyCls.Text = dmBacSyClsEntity.MaBacSy + " - " + dmBacSyClsEntity.TenBacSy;
        }

        private void btnAnh1_Click(object sender, EventArgs e)
        {
            txtAnh1.Text = FunctionList.FctOpenFileImage("Chọn hình 1");
        }

        private void btnAnh2_Click(object sender, EventArgs e)
        {
            txtAnh2.Text = FunctionList.FctOpenFileImage("Chọn hình 2");
        }

        private void btnAnh3_Click(object sender, EventArgs e)
        {
            txtAnh3.Text = FunctionList.FctOpenFileImage("Chọn hình 3");
        }

        private void btnAnh4_Click(object sender, EventArgs e)
        {
            txtAnh4.Text = FunctionList.FctOpenFileImage("Chọn hình 4");
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            ucChiDinhKhoaPhong.IsValidLookupEdit();
            ucChiDinhKhoaPhong.IsValidChooseDichVuChiTiet();

            if (resultInsert == false)
            {
                phieuThamDoChucNangEntity = new PhieuThamDoChucNangEntity()
                {
                    MaThamDoChucNang = _MaThamDoChucNang,
                    Stt = 0,
                    SoVaoVien = hoSoKhamBenhNgoaiTruEntity.SoVaoVien,
                    MaHoSo = hoSoKhamBenhNgoaiTruEntity.MaHoSo,
                    NgayVaoKham = hoSoKhamBenhNgoaiTruEntity.NgayVaoKham,
                    YeuCau = ucChiDinhKhoaPhong.getYeuCau(),
                    GhiChu = ucChiDinhKhoaPhong.getGhiChu(),
                    KetQua = txtKetQua.Html.Trim(),
                    KetLuan = txtKetLuan.Html.Trim(),
                    MaBacSyCls = string.Empty,
                    MaBacSyKham = Authenticate.MaBacSy,
                    Anh1 = string.Empty,
                    Anh2 = string.Empty,
                    Anh3 = string.Empty,
                    Anh4 = string.Empty,
                    TrangThaiPhieu = (int)ConstantManagerClinic.TrangThaiPhieuCanLamSang.TaoMoi,
                    MaKhoaCanLamSang = ucChiDinhKhoaPhong.getMaKhoaCanLamSang(),
                    MaPhongCanLamSang = ucChiDinhKhoaPhong.getMaPhongCanLamSang(),
                    MaDichVu = _MaDichVu,
                    MaKhoaKham = ucChiDinhKhoaPhong.getMaKhoaKham(),
                    MaPhongKham = ucChiDinhKhoaPhong.getMaPhongKham(),
                    TrangThaiCho = Convert.ToBoolean(ConstantManagerClinic.TrangThaiChoDichVuKhamChuaBenh.ChuaThucHien)
                };
                resultInsert = phieuThamDoChucNangBusiness.InsertPhieuThamDoChucNang(phieuThamDoChucNangEntity);
            }
            else
            {
                phieuThamDoChucNangEntity = phieuThamDoChucNangBusiness.GetPhieuThamDoChucNang(_MaThamDoChucNang);
                phieuThamDoChucNangEntity.YeuCau = ucChiDinhKhoaPhong.getYeuCau();
                phieuThamDoChucNangEntity.GhiChu = ucChiDinhKhoaPhong.getGhiChu();
                phieuThamDoChucNangEntity.KetQua = txtKetQua.Html.Trim();
                phieuThamDoChucNangEntity.KetLuan = txtKetQua.Html.Trim();
                phieuThamDoChucNangEntity.Anh1 = string.Empty;
                phieuThamDoChucNangEntity.Anh2 = string.Empty;
                phieuThamDoChucNangEntity.Anh3 = string.Empty;
                phieuThamDoChucNangEntity.Anh4 = string.Empty;
                phieuThamDoChucNangEntity.MaKhoaCanLamSang = ucChiDinhKhoaPhong.getMaKhoaCanLamSang();
                phieuThamDoChucNangEntity.MaPhongCanLamSang = ucChiDinhKhoaPhong.getMaPhongCanLamSang();
                phieuThamDoChucNangEntity.MaDichVu = _MaDichVu;
                phieuThamDoChucNangEntity.MaKhoaKham = ucChiDinhKhoaPhong.getMaKhoaKham();
                phieuThamDoChucNangEntity.MaPhongKham = ucChiDinhKhoaPhong.getMaPhongKham();

                phieuThamDoChucNangBusiness.EditPhieuThamDoChucNang(phieuThamDoChucNangEntity);
            }
            //delete from table ThuPhiDichVu
            thuPhiDichVuBusiness.DeleteThuPhiDichVuByMaPhieuDichVu(_MaThamDoChucNang);
            //delete from table PhieuThamDoChucNangChiTiet
            phieuThamDoChucNangChiTietBusiness.DeletePhieuThamDoChucNangChiTiet_ByMaThamDoChucNang(_MaThamDoChucNang);

            if (resultInsert == true)
            {
                //Insert into table ThuPhiDichVu, PhieuSieuAmChiTiet
                IList<DmDichVuChiTietEntity> lstDmDichVuChiTietIsChecked = ucChiDinhKhoaPhong.DmDichVuChiTietEntityIsChecked;
                if (lstDmDichVuChiTietIsChecked == null) { return; }

                foreach (DmDichVuChiTietEntity dmDichVuChiTietEntity in lstDmDichVuChiTietIsChecked)
                {
                    var thuphiDichVuEntity = new ThuPhiDichVuEntity()
                    {
                        MaPhieuDichVu = _MaThamDoChucNang,
                        MaHoSo = phieuThamDoChucNangEntity.MaHoSo,
                        NgayDenKham = phieuThamDoChucNangEntity.NgayVaoKham,
                        MaDichVu = _MaDichVu,
                        MaChiTieu = dmDichVuChiTietEntity.MaChiTieu,
                        TongTien = dmDichVuChiTietEntity.DonGia,
                        DaDongTien = (int)ConstantManagerClinic.TrangThaiDongTien.ChuaDongTien,
                        MaNguoiDung = Authenticate.UserName,
                        MaKhoaKham = phieuThamDoChucNangEntity.MaKhoaKham,
                        MaPhongKham = phieuThamDoChucNangEntity.MaPhongKham,
                        MaKhoaCanLamSang = phieuThamDoChucNangEntity.MaKhoaCanLamSang,
                        MaPhongCanLamSang = phieuThamDoChucNangEntity.MaPhongCanLamSang,
                        DaThucHien = Convert.ToBoolean(ConstantManagerClinic.TrangThaiThucHienDichVuKhamChuaBenh.ChuaThucHien)
                    };
                    thuPhiDichVuBusiness.InsertThuPhiDichVu(thuphiDichVuEntity);

                    var phieuThamDoChucNangChiTietEntity = new PhieuThamDoChucNangChiTietEntity()
                    {
                        MaThamDoChucNang = _MaThamDoChucNang,
                        MaChiTieu = dmDichVuChiTietEntity.MaChiTieu,
                        DonGia = dmDichVuChiTietEntity.DonGia,
                        DaThuTien = (int)ConstantManagerClinic.TrangThaiDongTien.ChuaDongTien
                    };
                    phieuThamDoChucNangChiTietBusiness.InsertPhieuThamDoChucNangChiTiet(phieuThamDoChucNangChiTietEntity);
                }
            }
            if (_SavePhieuCanLamSang != null) _SavePhieuCanLamSang(sender, e);
        }

        private void frmPhieuThamDoChucNang_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.SendWait("{Tab}");
            }
        }

        private void btnLuuKetQua_Click(object sender, EventArgs e)
        {
            FunctionList.FctCopyFile(Application.StartupPath, txtAnh1.Text.Trim(), _MaThamDoChucNang + "_Anh1");
            FunctionList.FctCopyFile(Application.StartupPath, txtAnh2.Text.Trim(), _MaThamDoChucNang + "_Anh2");
            FunctionList.FctCopyFile(Application.StartupPath, txtAnh3.Text.Trim(), _MaThamDoChucNang + "_Anh3");
            FunctionList.FctCopyFile(Application.StartupPath, txtAnh4.Text.Trim(), _MaThamDoChucNang + "_Anh4");

            phieuThamDoChucNangEntity = phieuThamDoChucNangBusiness.GetPhieuThamDoChucNang(_MaThamDoChucNang);
            phieuThamDoChucNangEntity.KetQua = txtKetQua.Html;
            phieuThamDoChucNangEntity.KetLuan = txtKetLuan.Html;
            phieuThamDoChucNangEntity.MaBacSyCls = Authenticate.MaBacSy;
            phieuThamDoChucNangEntity.Anh1 = txtAnh1.Text;
            phieuThamDoChucNangEntity.Anh2 = txtAnh2.Text;
            phieuThamDoChucNangEntity.Anh3 = txtAnh3.Text;
            phieuThamDoChucNangEntity.Anh4 = txtAnh4.Text;
            phieuThamDoChucNangBusiness.EditPhieuThamDoChucNang(phieuThamDoChucNangEntity);
            MessageBox.Show("Cập nhật thành công bản ghi!", ConstantManagerClinic.sTitleInformation, MessageBoxButtons.OK);
        }

        private void btnDongKetQua_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmPhieuThamDoChucNang_Load(object sender, EventArgs e)
        {
            ucChiDinhKhoaPhong.setDmDichVuChiTietEntityIsChecked(_MaThamDoChucNang, _MaDichVu);
        }
    }
}